package com.souku.finance.application.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.bean.RecordNotBean;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.widget.MyTextView;

import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class RecordAdapter extends BaseAdapter {

    private List<RecordNotBean.DataBean> listno;

    public RecordAdapter(List<RecordNotBean.DataBean> listno) {
        this.listno = listno;
    }

    @Override
    public int getCount() {
        return listno.size();
    }

    @Override
    public Object getItem(int position) {
        return listno.get(position).getId();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder = null;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_record, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        RecordNotBean.DataBean bean = listno.get(position);
        viewHolder.tvDealName.setText(bean.getDealName());
        if (Double.parseDouble(bean.getIncInterestRate()) == 0) {
            viewHolder.tvIncInterestRate.setVisibility(View.GONE);
            viewHolder.tvIncInterestRate.setText("");
        } else {
            viewHolder.tvIncInterestRate.setVisibility(View.VISIBLE);
            viewHolder.tvIncInterestRate.setText("加息" + bean.getIncInterestRate() + "%");
        }
        if (bean.getHasRepayed().equals("1")) {
            viewHolder.tvResidualDays.setText("已回款");
        } else {
//            viewHolder.tvResidualDays.setText(bean.getResidualDays() + "天到期");
            viewHolder.tvResidualDays.setText("未回款");
        }
        viewHolder.tvAmount.setText(MyTextUtil.setMoneyToElement(bean.getAmount()));
        viewHolder.tvInterest.setText(MyTextUtil.setMoneyToElement(bean.getInterest()));
        viewHolder.tvInvestDate.setText("投资时间" + MyTextUtil.changeTimeFormat(bean.getInvestDate()));
        viewHolder.tvPlanRepayDate.setText("到期时间" + MyTextUtil.changeTimeFormat(bean.getPlanRepayDate()));
        return view;
    }

    static class ViewHolder {
        protected TextView tvDealName;
        protected TextView tvIncInterestRate;
        protected TextView tvResidualDays;
        protected MyTextView tvAmount;
        protected MyTextView tvInterest;
        protected TextView tvInvestDate;
        protected TextView tvPlanRepayDate;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvDealName = (TextView) rootView.findViewById(R.id.tv_dealName);
            tvIncInterestRate = (TextView) rootView.findViewById(R.id.tv_incInterestRate);
            tvResidualDays = (TextView) rootView.findViewById(R.id.tv_residualDays);
            tvAmount = (MyTextView) rootView.findViewById(R.id.tv_amount);
            tvInterest = (MyTextView) rootView.findViewById(R.id.tv_interest);
            tvInvestDate = (TextView) rootView.findViewById(R.id.tv_investDate);
            tvPlanRepayDate = (TextView) rootView.findViewById(R.id.tv_planRepayDate);
        }
    }
}
